package com.zjut.zs.fileupload.action;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;
import com.zjut.an.config.manager.AnConfigManager;


import javax.servlet.http.HttpSession;



public class ZsFileUploadAction extends ActionSupport implements
		ServletResponseAware, ServletRequestAware {

	private HttpServletRequest request;
	private HttpServletResponse response;
	private HttpSession session;


	private Map<String, Object> dataMap;


	private File[] file; // 上传的文件
	private String[] fileFileName; // 文件名称
	private String[] fileContentType; // 文件类型
	private String idcard;
	private String fileFileNames = ""; // 文件名称

	@Resource(name = "AnConfigManager")
	private AnConfigManager anconfig;


	

	public String zsFileUpload() throws IOException {
		dataMap = new HashMap<String, Object>();

		request.setCharacterEncoding("UTF-8");
		// idcard = request.getParameter("idcard");
		String savePath = "d:\\filedownload\\" + idcard + "\\";
		if (null != anconfig.getSingleConfigInfoByName("佐证材料保存路径")) {
			savePath = anconfig.getSingleConfigInfoByName("佐证材料保存路径") + "\\"
					+ idcard + "\\";
		}
		System.out.println(savePath);
		if (file != null) {
			File savedir = new File(savePath);
			if (!savedir.getParentFile().exists())
				savedir.getParentFile().mkdirs();
			for (int i = 0; i < file.length; i++) {
				if (i == file.length - 1) {
					fileFileNames += savePath+fileFileName[i];
				} else {
					fileFileNames += savePath+fileFileName[i] + '|';
				}
				File savefile = new File(savedir, fileFileName[i]);
				FileUtils.copyFile(file[i], savefile);
			}
			dataMap.put("flag", "success");
			dataMap.put("fiFile", fileFileNames);
			return SUCCESS;

		}else{
			return ERROR;
		}

		
	}



	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		request = arg0;
		session = request.getSession();
	}

	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		response = arg0;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public HttpSession getSession() {
		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}



	public Map<String, Object> getDataMap() {
		return dataMap;
	}

	public void setDataMap(Map<String, Object> dataMap) {
		this.dataMap = dataMap;
	}


	public File[] getFile() {
		return file;
	}

	public void setFile(File[] file) {
		this.file = file;
	}

	public String[] getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String[] fileFileName) {
		this.fileFileName = fileFileName;
	}

	public String[] getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String[] fileContentType) {
		this.fileContentType = fileContentType;
	}

	public String getFileFileNames() {
		return fileFileNames;
	}

	public void setFileFileNames(String fileFileNames) {
		this.fileFileNames = fileFileNames;
	}

	public String getIdcard() {
		return idcard;
	}

	public void setIdcard(String idcard) {
		this.idcard = idcard;
	}

}
